<template>
  <div id="homePage" class="home-page-container">
    <!-- 梦幻动态背景 -->
    <div class="dream-background">
      <!-- 渐变光晕 -->
      <div class="gradient-orb orb1"></div>
      <div class="gradient-orb orb2"></div>
      <div class="gradient-orb orb3"></div>
      <div class="gradient-orb orb4"></div>

      <!-- 动态粒子 -->
      <div class="particles-container">
        <div
          v-for="n in 50"
          :key="n"
          class="particle"
          :style="{
            '--delay': `${Math.random() * 20}s`,
            '--size': `${Math.random() * 6 + 2}px`,
            '--start-x': `${Math.random() * 100}%`,
            '--start-y': `${Math.random() * 100}%`,
            '--color': `hsl(${Math.random() * 60 + 20}, ${Math.random() * 40 + 60}%, ${
              Math.random() * 20 + 70
            }%)`,
          }"
        ></div>
      </div>

      <!-- 动态气泡 -->
      <div class="bubbles-container">
        <div
          v-for="n in 20"
          :key="n"
          class="bubble"
          :style="{
            '--delay': `${Math.random() * 15}s`,
            '--size': `${Math.random() * 100 + 30}px`,
            '--start-x': `${Math.random() * 100}%`,
            '--color': `hsla(${Math.random() * 60 + 20}, 100%, 75%, 0.1)`,
          }"
        ></div>
      </div>
    </div>

    <div class="content-container">
      <!-- 过滤器容器 -->
      <div class="filter-container">
        <div class="filter-left">
          <div class="category-tabs">
            <a-tabs v-model:active-key="selectedCategory" @change="doSearch" class="custom-tabs">
              <a-tab-pane key="all" tab="全部" />
              <a-tab-pane v-for="category in categoryList" :tab="category" :key="category" />
            </a-tabs>
          </div>

          <div class="tag-container">
            <div class="tag-bar">
              <span class="tag-label">标签：</span>
              <a-space :size="[0, 8]" wrap class="tag-cloud">
                <a-checkable-tag
                  v-for="(tag, index) in tagList"
                  :key="tag"
                  v-model:checked="selectedTagList[index]"
                  @change="doSearch"
                  class="custom-tag"
                >
                  {{ tag }}
                </a-checkable-tag>
              </a-space>
            </div>
          </div>
        </div>

        <div class="filter-right">
          <div class="search-bar">
            <a-input-search
              v-model:value="searchParams.searchText"
              placeholder="从海量图片中搜索"
              enter-button="搜索"
              @search="doSearch"
              class="custom-search"
            />
          </div>
        </div>
      </div>

      <!-- 图片列表 -->
      <div class="picture-list-container">
        <PictureList :dataList="dataList" :loading="loading" />
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <a-pagination
          v-model:current="searchParams.current"
          v-model:pageSize="searchParams.pageSize"
          :total="total"
          @change="onPageChange"
          class="custom-pagination"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue' // 定义数据

// 定义数据
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 30,
  sortField: 'createTime',
  sortOrder: 'descend',
})

/**
 * 获取图片列表数据
 */
const fetchData = async () => {
  loading.value = true
  const res = await listPictureVoByPageUsingPost(searchParams)
  if (res.data.code === 0) {
    dataList.value = res.data.data?.records ?? []
    total.value = res.data.data?.total ?? 0
  } else {
    message.error('获取图片列表失败，' + res.data.message)
  }
  loading.value = false
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
})

// 翻页
const onPageChange = () => {
  fetchData()
}

// 搜索
const doSearch = () => {
  // 重置页码
  searchParams.current = 1
  // 添加选中的标签到搜索参数
  const selectedTags = tagList.value.filter((_, index) => selectedTagList.value[index])
  searchParams.tags = selectedTags
  // 添加选中的分类
  if (selectedCategory.value !== 'all') {
    searchParams.category = selectedCategory.value
  } else {
    searchParams.category = undefined
  }
  fetchData()
}

// 标签和分类列表
const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

/**
 * 获取标签和分类选项
 * @param values
 */
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    tagList.value = res.data.data.tagList ?? []
    categoryList.value = res.data.data.categoryList ?? []
  } else {
    message.error('获取标签分类列表失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style scoped>
/* 整体容器背景 */
.home-page-container {
  position: relative;
  min-height: 100vh;
  overflow-x: hidden;
  background: linear-gradient(135deg, #fff6f0, #ffe4d4);
}

/* 梦幻背景 */
.dream-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  overflow: hidden;
  background: linear-gradient(
    135deg,
    rgba(255, 246, 240, 0.9),
    rgba(255, 228, 212, 0.9),
    rgba(255, 212, 184, 0.9)
  );
}

/* 渐变光晕 */
.gradient-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.4;
  mix-blend-mode: soft-light;
  animation: floatOrb 20s infinite ease-in-out;
}

.orb1 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle at 30% 30%, #ff9f43, rgba(255, 138, 101, 0));
  top: -10%;
  left: -10%;
  animation-duration: 25s;
}

.orb2 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle at 70% 30%, #4facfe, rgba(79, 172, 254, 0));
  top: 40%;
  right: -10%;
  animation-duration: 30s;
  animation-delay: 2s;
}

.orb3 {
  width: 450px;
  height: 450px;
  background: radial-gradient(circle at 50% 50%, #a8edea, rgba(168, 237, 234, 0));
  bottom: -10%;
  left: 20%;
  animation-duration: 28s;
  animation-delay: 5s;
}

.orb4 {
  width: 550px;
  height: 550px;
  background: radial-gradient(circle at 30% 70%, #fed6e3, rgba(254, 214, 227, 0));
  top: 30%;
  left: 40%;
  animation-duration: 32s;
  animation-delay: 3s;
}

/* 动态粒子 */
.particles-container {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.particle {
  position: absolute;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: var(--color);
  left: var(--start-x);
  top: var(--start-y);
  animation: floatParticle var(--delay) infinite linear;
  opacity: 0.6;
}

@keyframes floatParticle {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 0;
  }
  20% {
    opacity: 0.6;
  }
  80% {
    opacity: 0.6;
  }
  100% {
    transform: translate(calc(sin(var(--delay)) * 200px), calc(cos(var(--delay)) * 200px))
      rotate(360deg);
    opacity: 0;
  }
}

/* 动态气泡 */
.bubbles-container {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.bubble {
  position: absolute;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  bottom: -100px;
  left: var(--start-x);
  background: radial-gradient(circle at 30% 30%, var(--color), rgba(255, 255, 255, 0.05));
  backdrop-filter: blur(2px);
  animation: rise calc(var(--delay) * 2) infinite ease-in;
  box-shadow: inset 0 0 20px rgba(255, 255, 255, 0.3);
}

@keyframes rise {
  0% {
    transform: translateY(0) scale(1) rotate(0deg);
    opacity: 0;
  }
  50% {
    opacity: 0.5;
    transform: translateY(-50vh) scale(1.2) rotate(180deg);
  }
  100% {
    transform: translateY(-100vh) scale(0.8) rotate(360deg);
    opacity: 0;
  }
}

@keyframes floatOrb {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(-30px, 30px) scale(1.1);
  }
  50% {
    transform: translate(30px, -20px) scale(0.9);
  }
  75% {
    transform: translate(20px, 30px) scale(1.05);
  }
}

/* 确保内容在背景之上 */
.content-container {
  position: relative;
  z-index: 1;
  backdrop-filter: blur(5px);
  min-height: 100vh;
}

/* 过滤器容器布局调整 */
.filter-container {
  background: rgba(255, 255, 255, 0.9);
  margin: 0 auto 12px;
  border-radius: 12px;
  padding: 24px;
  margin-top: 10px;
  display: flex;
  gap: 24px;
  position: relative;
  z-index: 10;
}

.filter-left {
  flex: 1;
  min-width: 0;
}

.filter-right {
  width: 300px;
  display: flex;
  align-items: flex-start;
  padding-top: 8px;
}

/* 搜索框样式调整 */
.search-bar {
  width: 100%;
}

.custom-search :deep(.ant-input) {
  height: 40px;
  border-radius: 20px;
  padding-left: 16px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 159, 67, 0.2);
  box-shadow: 0 4px 12px rgba(31, 38, 135, 0.08);
  transition: all 0.3s;
}

.custom-search :deep(.ant-input-search-button) {
  height: 40px;
  width: 70px;
  border-radius: 0 20px 20px 0;
  background: linear-gradient(135deg, #ff9f43 0%, #ff7f50 100%);
  border: none;
  font-size: 14px;
  font-weight: 500;
}

/* 分类标签调整 */
.category-tabs {
  margin-bottom: 20px;
}

/* 标签栏样式调整 */
.tag-container {
  position: relative;
  z-index: 10;
}

.tag-bar {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  position: relative;
  z-index: 5;
}

.tag-label {
  font-weight: 500;
  color: #2d3436;
  line-height: 32px;
  min-width: 56px; /* 固定标签文字宽度 */
}

/* 标签样式优化 */
.custom-tag {
  margin: 0 8px 8px 0;
  padding: 6px 16px;
  border-radius: 100px;
  font-size: 14px;
  transition: all 0.3s;
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid #ffe4d4;
  color: #666;
  cursor: pointer;
}

/* 标签悬停效果 */
.custom-tag:hover {
  transform: translateY(-2px);
  background: rgba(255, 243, 224, 0.8);
  border-color: #ffd4b8;
  color: #ff9f43;
}

/* 标签选中样式 - 修改这部分确保选中状态持续 */
.custom-tag.ant-tag-checkable-checked {
  background: linear-gradient(135deg, #ff9f43, #ff7f50) !important;
  border: none !important;
  color: white !important;
  box-shadow: 0 4px 12px rgba(255, 159, 67, 0.2);
}

/* 自定义标签页样式调整 */
.custom-tabs :deep(.ant-tabs-tab) {
  padding: 12px 20px;
  margin: 0 12px 0 0;
  font-size: 15px;
  transition: all 0.3s;
}

.custom-tabs :deep(.ant-tabs-tab:hover) {
  color: #ff9f43;
}

.custom-tabs :deep(.ant-tabs-tab-active) {
  color: #f57c00 !important;
}

.custom-tabs :deep(.ant-tabs-ink-bar) {
  background: linear-gradient(90deg, #ff9f43, #f57c00);
}

/* 图片列表容器 */
.picture-list-container {
  margin: 0 -8px; /* 减少图片列表的左右间距 */
  animation: fadeInUp 0.8s ease-out;
  animation-delay: 0.3s;
  animation-fill-mode: both;
}

/* 分页容器 */
.pagination-container {
  position: sticky;
  bottom: 20px;
  margin-top: 40px;
  text-align: center;
  padding: 16px 0;
  z-index: 10;
}

.custom-pagination {
  display: inline-flex;
  background: rgba(255, 255, 255, 0.95);
  padding: 8px 16px;
  border-radius: 100px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(8px);
}

/* 动画 */
@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式调整 */
@media (max-width: 1440px) {
  .content-container {
    max-width: 1200px;
  }
}

@media (max-width: 1200px) {
  .content-container {
    max-width: 960px;
  }
}

@media (max-width: 992px) {
  .filter-container {
    flex-direction: column;
  }

  .filter-right {
    width: 100%;
    padding-top: 0;
  }

  .search-bar {
    max-width: 100%;
  }
}

@media (max-width: 768px) {
  .content-container {
    padding: 0 12px;
  }

  .search-bar {
    max-width: 100%;
  }

  .filter-container {
    margin: 0 8px 24px;
  }
}
</style>
